package com.linkin.homepage.base.recycleview;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;


public class BaseItemDecoration extends RecyclerView.ItemDecoration {
    private int mSpace;
    private boolean mShowFirst;
    private boolean mShowLast;
    private boolean mHorizontal;

    public BaseItemDecoration(int space) {
        this(space, true, true, false);
    }

    public BaseItemDecoration(int space, boolean horizontal) {
        this(space, true, true, horizontal);
    }

    public BaseItemDecoration(int space, boolean showFirst, boolean showLast, boolean horizontal) {
        this.mSpace = space;
        this.mShowFirst = showFirst;
        this.mShowLast = showLast;
        this.mHorizontal = horizontal;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        RecyclerView.Adapter adapter = parent.getAdapter();
        if (adapter == null) {
            return;
        }

        int itemCount;
        if (adapter instanceof RCWrapperAdapter) {
            RecyclerView.Adapter realAdapter = ((RCWrapperAdapter)adapter).getInnerAdapter();
            if (realAdapter == null) {
                return;
            }

            position = position - ((RCWrapperAdapter)adapter).getHeaderViewsCount();
            itemCount = realAdapter.getItemCount();
        } else {
            itemCount = adapter.getItemCount();
        }

        if (mShowLast) {
            if (mShowFirst && position == 0) {
                if (mHorizontal) {
                    outRect.left = mSpace;
                } else {
                    outRect.top = mSpace;
                }
            } else {
                if (mHorizontal) {
                    outRect.left = 0;
                } else {
                    outRect.top = 0;
                }
            }

            if (position >= 0 && position < itemCount) {
                if (mHorizontal) {
                    outRect.right = mSpace;
                } else {
                    outRect.bottom = mSpace;
                }
            } else {
                if (mHorizontal) {
                    outRect.right = 0;
                } else {
                    outRect.bottom = 0;
                }
            }
        } else {
            if (mShowFirst && position == 0
                    || position > 0 && position < itemCount) {
                if (mHorizontal) {
                    outRect.left = mSpace;
                } else {
                    outRect.top = mSpace;
                }
            } else {
                if (mHorizontal) {
                    outRect.left = 0;
                } else {
                    outRect.top = 0;
                }
            }
        }
    }

    private Paint mPaint = null;
    private Rect mBounds = new Rect();
    private Rect mTempRect = new Rect();

    public void setDividerColor(int color) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
    }

    private int mDividerMarginLeft = 0;
    private int mDividerMarginRight = 0;

    public void setDividerMarginLeft(int dividerMarginLeft) {
        mDividerMarginLeft = dividerMarginLeft;
    }

    public void setDividerMarginRight(int dividerMarginRight) {
        mDividerMarginRight = dividerMarginRight;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() == null || mPaint == null) {
            return;
        }
        if (mHorizontal) {
            drawHorizontal(c, parent, state);
        } else {
            drawVertical(c, parent, state);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        canvas.save();
        final int left;
        final int right;
        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft() + mDividerMarginLeft;
            right = parent.getWidth() - parent.getPaddingRight() - mDividerMarginRight;
            canvas.clipRect(left, parent.getPaddingTop(), right,
                    parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = mDividerMarginLeft;
            right = parent.getWidth() - mDividerMarginRight;
        }

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            parent.getDecoratedBoundsWithMargins(child, mBounds);

            mTempRect.setEmpty();
            getItemOffsets(mTempRect, child, parent, state);

            if (mTempRect.top > 0) {
                final int top = mBounds.top + Math.round(child.getTranslationY());
                final int bottom = top + mSpace;
                canvas.drawRect(left, top, right, bottom, mPaint);
            }

            if (mTempRect.bottom > 0) {
                final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
                final int top = bottom - mSpace;
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
        canvas.restore();
    }

    private void drawHorizontal(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        canvas.save();
        final int top;
        final int bottom;
        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            top = parent.getPaddingTop();
            bottom = parent.getHeight() - parent.getPaddingBottom();
            canvas.clipRect(parent.getPaddingLeft(), top,
                    parent.getWidth() - parent.getPaddingRight(), bottom);
        } else {
            top = 0;
            bottom = parent.getHeight();
        }

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);

            mTempRect.setEmpty();
            getItemOffsets(mTempRect, child, parent, state);

            if (mTempRect.left > 0) {
                final int left = mBounds.left + Math.round(child.getTranslationX());
                final int right = left + mSpace;
                canvas.drawRect(left, top, right, bottom, mPaint);
            }

            if (mTempRect.right > 0) {
                final int right = mBounds.right + Math.round(child.getTranslationX());
                final int left = right - mSpace;
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
        canvas.restore();
    }

}
